// component/country/country.js
const app = getApp()
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    
  },
  observers: {

  },
  lifetimes: {
    ready() {
      this.get_country()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    search_value: '',
    o_index: -1,
    columns: [],
    loading: false,
    change_value: '',
    old_data: [],
    hot_list: [],
    columns_index:0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show_demo(){
      this.setData({
        show:true
      })
    },
    onClose() {
      this.setData({
        show: false
      })
    },
    get_index(e) {
      let index = e.currentTarget.dataset.index;
      if (index == this.data.o_index) {
        this.setData({
          o_index: -1
        })
        this.setData({
          columns:this.data.old_data,
          columns_index:0
        })
      }else{
        this.setData({
          o_index: e.currentTarget.dataset.index
        })
        let title=this.data.hot_list[this.data.o_index].name;
        this.setData({
          columns:this.data.old_data.filter(item=>{return item.name.includes(title)}),
          columns_index:0
        })
      }
    },
    // 筛选城市
    cloum_country(){
      const that=this;
    },
    onChanges(event) {
      const { picker, value, index } = event.detail;
      this.setData({
        columns_index: index
      })
    },
    confirm() {
      if(this.data.columns.length>0){
        this.triggerEvent('confirm_country', { value: this.data.columns[this.data.columns_index].code,title:this.data.columns[this.data.columns_index].name })
        this.setData({
          show:false
        })
      }
    },
    onChange(e) {
      let title=e.detail;
      if(title){
        this.setData({
          columns:this.data.old_data.filter(item=>{return item.name.includes(title)}),
          columns_index:0
        })
      }else{
        this.setData({
          columns:this.data.old_data,
          columns_index:0
        })
      }
      
    },
    get_country() {
      const that = this;
      that.setData({
        loading: true
      })
      app.wxRequest('/api/Pack/getCountryList', {}, function (res) {
        if (res.code == 200) {
          let data = res.data;
          that.setData({
            old_data: data,
            loading: false,
            columns: data,
            hot_list: data.filter(item => { return item.hot == 1 })
          })
          that.triggerEvent('country_list',{value:res.data})
        } else {
          app.toast(res.message)
        }
      }, 'get')
    },
  }
})